
	<h2>Estadísticas</h2>
	
	<!-- 
		Posible candidato en lugar de Google Charts: http://d3js.org 
		Ejemplo: http://bl.ocks.org/1667367
	-->
	
<link href="css/nv.d3.css" rel="stylesheet" type="text/css">

<div id="chart">
	<svg style="height:500px"></svg>
</div>
  
<script src="js/stream_layers.js"></script>  
<script src="js/d3.v2.min.js"></script>  
<script src="js/nv.d3.js"></script>

<script>
nv.addGraph(function() {
  var chart = nv.models.lineWithFocusChart();

  chart.xAxis
      .tickFormat(d3.format(',f'));
  chart.x2Axis
      .tickFormat(d3.format(',f'));

  chart.yAxis
      .tickFormat(d3.format(',.2f'));
  chart.y2Axis
      .tickFormat(d3.format(',.2f'));

  d3.select('#chart svg')
      .datum(sinAndCos())
    .transition().duration(500)
      .call(chart);

  nv.utils.windowResize(chart.update);

  return chart;
});

/**************************************
 * Simple test data generator
 */


function sinAndCos() {
  var sin = [],
      cos = [];

  for (var i = 0; i < 100; i++) {
    sin.push({x: i, y: Math.sin(i/20)});
    cos.push({x: i, y: .5 * Math.cos(i/10)});
  }

  return [
    {
      values: sin,
      key: 'Ventas',
      color: '#ff7f0e'
    },
    {
      values: cos,
      key: 'Compras',
      color: '#2ca02c'
    }
  ];
}
/*
function testData() {
  return stream_layers(3,128,.1).map(function(data, i) {
    return { 
      key: 'Stream' + i,
      values: data
    };
  });
}
*/

</script>

<!-- -->	
	
	
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
	<div id="chart_div" style="width: 100%; height: 500px;"></div>

	
<?php		

		// Recuperamos los datos de ventas
		$facturacion = new epFacturacionclass();
		$ventas = $facturacion->getVentasProducto($producto['referencia']);
	
		echo "<pre>";
	
		$data = array();
		/*
		for ($i=0 ; $i<100 ; $i++)
		{
			array_push($data, array($i, rand($i,100*($i*100))));
		}
		*/

		$i=0;
		$val_max = 0;
		foreach ($ventas as $venta) {
			array_push($data, array($i, $venta['cantidad']));
			if ($venta['cantidad'] > $val_max) { $val_max = $venta['cantidad']; } // Guardamos el valor máximo dentro de los datos
			$i++;
		}
					
		$reg = new Regression($data);
		$ret = $reg->generateAllCurves();
		//var_dump($reg);				
				
		echo "\n";
		
		$acc = $reg->calculateCoefficients($reg->most_accurate_method);

		var_dump($acc);
		
		echo "Most acurate: ". $reg->most_accurate_method ."\n";
		echo "Coefficients: ". $reg->curves[$reg->most_accurate_method]['coefficients']['A'] .", ". $reg->curves[$reg->most_accurate_method]['coefficients']['B'] ."\n";
		echo "Accuracy: ". $reg->curves[$reg->most_accurate_method]['accuracy'] ."\n";

		echo "Evaluado:\n";
		
		echo "
			<script type=\"text/javascript\">
			  google.load(\"visualization\", \"1\", {packages:[\"corechart\"]});
			  google.setOnLoadCallback(drawChart);
			  function drawChart() {
				var data = google.visualization.arrayToDataTable([
				  ['Dia', 'Ventas', 'Predicción'],\n
				  ";
			

			foreach ($data as $a => $b)
			{
				$eval = $reg->evaluateMostAccurateCurve($b[0]);				
				$accum .= "['". $b[0] ."',  ". $b[1] .", ". round($eval,0) ."],\n";				
				$total_eval += $eval;
			}					

			$tiempo_entrega=10; // dias
			$suma = $b[0];
			while ($tiempo_entrega--)
			{							
				$suma++;
				$eval = $reg->evaluateMostAccurateCurve($suma);				
				$accum .= "['". $suma ."',  ". 0 .", ". round($eval,0) ."],\n";												
			}
			
			echo $accum;
		
		echo "\n
				]);

				var options = {
				  title: 'Gráfico de ventas'
				};

				var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
				chart.draw(data, options);
			  }
			</script>		
		";		
				
		echo "</pre>";

?>